﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AgentBar.Common;
using System.Configuration;
using Cpfmsgacxa;

namespace AgentBar
{
    public partial class frmPhoneCome : Form
    {
        public string ty { get; set; }
        public string ln { get; set; }
        private CpfParams objOffParams;
        private frmAgentBar MainF;
        [System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true, ThrowOnUnmappableChar = true)]
        private static extern bool PlaySound(string szsound, System.IntPtr hmod, playSound flag);
        private string Language
        {
            get
            {
                string language = ConfigurationManager.AppSettings["LAN"];
                if (string.IsNullOrEmpty(language)) language = "JP";
                return language;
            }
        }
        public frmPhoneCome(frmAgentBar ParentFrm)
        {
            InitializeComponent();
            MainF = ParentFrm;
            this.objOffParams = MainF.g_objOffParams;
        }

        private void frmPhoneCome_Load(object sender, EventArgs e)
        {
            try
            {
                CommonFunction.WriteLog("frmPhoneCome_Load Started");
                this.lblNo.Text = objOffParams.GetStringDefault("vCallerID", "");
                this.lblGroup.Text = objOffParams.GetStringDefault("vIncmGroup", "");
                this.lblSkillGroup.Text = objOffParams.GetStringDefault("vDistGroup", "");
                this.lblInfo.Text = objOffParams.GetStringDefault("vCustTag", "");

                LanguageResourceManager _ResourceManager = new LanguageResourceManager(Language);
                PicBoxStatus picOk = new PicBoxStatus() { Title = "許可", BackGroudImage = Image.FromFile(Application.StartupPath + @"\Image\ButtonOK.png"), PictureBox = this.picAllow, Enabled = true };
                PicBoxStatus piccancel = new PicBoxStatus() { Title = "拒否", BackGroudImage = Image.FromFile(Application.StartupPath + @"\Image\ButtonCancel.png"), PictureBox = this.picReject, Enabled = true };
                PictureBoxTool.DrawInPictureBox(picOk);
                PictureBoxTool.DrawInPictureBox(piccancel);
                OpenPage();
                PlaySound();
                CommonFunction.WriteLog("frmPhoneCome_Load Ended");
            }
            catch (Exception ex)
            {
                CommonFunction.WriteLog(ex.Message);
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void OpenPage()
        {
            string vTranID = objOffParams.GetStringDefault("vTranID", "");
            string iMatterID = objOffParams.GetStringDefault("iMatterID", "");
            string iCustomerID = objOffParams.GetStringDefault("iCustomerID", "");
            string iMediaType = objOffParams.GetStringDefault("iMediaType", "");
            string vCalleeID = objOffParams.GetStringDefault("vCalleeID", "");
            string vCallerID = objOffParams.GetStringDefault("vCallerID", "");
            string iCustTag1 = objOffParams.GetStringDefault("iCustTag1", "");
            string iCustTag2 = objOffParams.GetStringDefault("iCustTag2", "");
            string vCustTag = objOffParams.GetStringDefault("vCustTag", "");
            string vDistGroup = objOffParams.GetStringDefault("vDistGroup", "");
            string vIncmGroup = objOffParams.GetStringDefault("vIncmGroup", "");
            string vFuncType = objOffParams.GetStringDefault("vFuncType", "");
            string iTransferID = objOffParams.GetStringDefault("iTransferID", "");
            string iSessionProfileID = objOffParams.GetStringDefault("iSessionProfileID", "");
            string iTimeout = objOffParams.GetStringDefault("iTimeout", "0");
            string vDisplay = objOffParams.GetStringDefault("vDisplay", "");
            string URL = objOffParams.GetStringDefault("sURL", "");

            URL = URL.Replace("#vTranID#", vTranID);
            URL = URL.Replace("#iMatterID#", iMatterID);
            URL = URL.Replace("#iCustomerID#", iCustomerID);
            URL = URL.Replace("#iMediaType#", iMediaType);
            URL = URL.Replace("#vCalleeID#", vCalleeID);
            URL = URL.Replace("#vCallerID#", vCallerID);
            URL = URL.Replace("#iCustTag1#", iCustTag1);
            URL = URL.Replace("#iCustTag2#", iCustTag2);
            URL = URL.Replace("#vCustTag#", vCustTag);
            URL = URL.Replace("#vDistGroup#", vDistGroup);
            URL = URL.Replace("#vIncmGroup#", vIncmGroup);
            URL = URL.Replace("#vFuncType#", vFuncType);
            URL = URL.Replace("#iTransferID#", iTransferID);
            URL = URL.Replace("#iSessionProfileID#", iSessionProfileID);
            URL = URL.Replace("#iTimeout#", iTimeout);
            URL = URL.Replace("#vDisplay#", vDisplay);

            if (!string.IsNullOrEmpty(URL))
            {
                System.Diagnostics.Process.Start(URL);
            }
            int result = 0;
            bool isNum = int.TryParse(iTimeout, out result);
            if (isNum)
            {
                _Timer.Interval = result * 1000;
                _Timer.Tick += new EventHandler(_Timer_Tick);
                _Timer.Enabled = true;
            }
        }

        void _Timer_Tick(object sender, EventArgs e)
        {
            autoTimeOut();
        }

        private void PlaySound()
        {
            PlaySound(Application.StartupPath + @"\sound\ring.wav", new IntPtr(), playSound.SND_LOOP | playSound.SND_ASYNC);
        }

        private void autoTimeOut()
        {
            MainF.SmOfferingNak();
            PlaySound(null, new IntPtr(), playSound.SND_ASYNC);
            this.Dispose();
        }

        private void picAllow_Click(object sender, EventArgs e)
        {
            try
            {
                _Timer.Enabled = false;
                MainF.SmOfferingAck();
                PlaySound(null, new IntPtr(), playSound.SND_ASYNC);
                this.Dispose();
            }
            catch (Exception ex)
            {
                //alert(e.description);
            }
        }

        private void picReject_Click(object sender, EventArgs e)
        {
            MainF.SmOfferingNak();
            PlaySound(null, new IntPtr(), playSound.SND_ASYNC);
            this.Dispose();
        }

        private void frmPhoneCome_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                picAllow_Click(null, null);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                picReject_Click(null, null);
            }
        }

        private void frmPhoneCome_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void pic_MouseHover(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void pic_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }
    }
}
